package com.one.controller;

import com.one.pojo.CleanRecord;
import com.one.pojo.LandRecord;
import com.one.pojo.RateDailyRecord;
import com.one.service.*;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 土地记录管理
 * @Author semstouch
 * @Date 2017/4/11
 **/
@Controller
@ControllerAdvice
public class LandRecordController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private LandRecordService landRecordService;
    @Resource
    private CleanRcordService cleanRcordService;
    @Resource
    private RateDailyRecordService rateDailyRecordService;
    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;

    public LandRecordController(){
        super("callback");
    }

    @RequestMapping(value = "/cleanRecord", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult cleanRecord(HttpServletRequest request,String token,String  userId,Integer currentPage, Integer numPerPage) {
        JSONResult jsonResult = new JSONResult();
        if (!cacheProvider.get(userId).equals(token)) {
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Map rs=new HashMap();
        Integer totalCount = cleanRcordService.countNum(userId);
        List<CleanRecord> recordList =cleanRcordService.findByCleanId(userId,numPerPage, currentPage);

        rs.put("currentPage", currentPage);
        rs.put("numPerPage", numPerPage);
        rs.put("totalCount", totalCount);
        rs.put("recordList", recordList);
        rs.put("pageCount", totalCount % numPerPage == 0 ? totalCount / numPerPage : totalCount / numPerPage + 1);
        rs.put("beginPageIndex", 1);
        rs.put("beginRowIndex", 0);
        rs.put("countResultMap", null);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    @RequestMapping(value = "/incubateEggRecord", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult incubateEggRecord(HttpServletRequest request,String token,String  userId,Integer currentPage, Integer numPerPage) {
        JSONResult jsonResult = new JSONResult();
        if (!cacheProvider.get(userId).equals(token)) {
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Map rs=new HashMap();
        Integer totalCount = landRecordService.countNum(userId, "2");
        List<LandRecord> recordList =landRecordService.findByUserId(userId, "2",numPerPage, currentPage);

        rs.put("currentPage", currentPage);
        rs.put("numPerPage", numPerPage);
        rs.put("totalCount", totalCount);
        rs.put("recordList", recordList);
        rs.put("pageCount", totalCount % numPerPage == 0 ? totalCount / numPerPage : totalCount / numPerPage + 1);
        rs.put("beginPageIndex", 1);
        rs.put("beginRowIndex", 0);
        rs.put("countResultMap", null);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    @RequestMapping(value = "/chickenToLandRecord", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult chickenToLandRecord(HttpServletRequest request,String token,String  userId,Integer currentPage, Integer numPerPage) {
        JSONResult jsonResult = new JSONResult();
        if (!cacheProvider.get(userId).equals(token)) {
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Map rs=new HashMap();
        Integer totalCount = landRecordService.countNum(userId, "1");
        List<LandRecord> recordList =landRecordService.findByUserId(userId, "1",numPerPage, currentPage);

        rs.put("currentPage", currentPage);
        rs.put("numPerPage", numPerPage);
        rs.put("totalCount", totalCount);
        rs.put("recordList", recordList);
        rs.put("pageCount", totalCount % numPerPage == 0 ? totalCount / numPerPage : totalCount / numPerPage + 1);
        rs.put("beginPageIndex", 1);
        rs.put("beginRowIndex", 0);
        rs.put("countResultMap", null);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    /**
     * 生长记录
     * @param request
     * @param token
     * @param userId
     * @param currentPage
     * @param numPerPage
     * @return
     */
    @RequestMapping(value = "/getRateDailyRecord", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getRateDailyRecord(HttpServletRequest request,String token,String  userId,Integer currentPage, Integer numPerPage) {
        JSONResult jsonResult = new JSONResult();
        if (!cacheProvider.get(userId).equals(token)) {
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Map rs=new HashMap();
        Integer totalCount = rateDailyRecordService.countNum(userId);
        List<RateDailyRecord> recordList =rateDailyRecordService.findByUserId(userId,numPerPage, currentPage);

        rs.put("currentPage", currentPage);
        rs.put("numPerPage", numPerPage);
        rs.put("totalCount", totalCount);
        rs.put("recordList", recordList);
        rs.put("pageCount", totalCount % numPerPage == 0 ? totalCount / numPerPage : totalCount / numPerPage + 1);
        rs.put("beginPageIndex", 1);
        rs.put("beginRowIndex", 0);
        rs.put("countResultMap", null);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

}
